package com.cm.sys.intermanager.controller;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.application.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cm.sys.framework.aop.NotCheckIP;
import com.cm.sys.framework.aop.NotCheckLogin;
import com.cm.sys.intermanager.service.WebSocketServer;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassNameWebSocketController
 * @Description 实时推送控制层
 * @Author jhx @Date2019/12/14 14:55
 * @Version V1.0
 **/
@Controller
@RequestMapping("/front")
public class WebSocketController {
    @Autowired
    private WebSocketServer webSocketServer;

    @PostMapping("/push")
    @NotCheckLogin
    @NotCheckIP
    @ResponseBody
    public ResponseResult getWebSocket(@RequestBody List<Map<String,String>> logs){
        if(webSocketServer.getWebSocketMap()!=null && !webSocketServer.getWebSocketMap().isEmpty()){
            try {
                Map<String, WebSocketServer> wsmap = webSocketServer.getWebSocketMap();
                for (Map<String, String> log : logs) {
                    String wsId = log.get("wsId");
                    for (String wskey : wsmap.keySet()) {
                        if(wskey.equals(wsId)){
                            wsmap.get(wskey).sendMsg(log.get("message"),wsmap.get(wskey).getSession());
                        }
                    }
                }
                return new ResponseResult(ApiResponseCode.SUCCESS);
            }  catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new ResponseResult(ApiResponseCode.NO_SESSION);
    }



}
